﻿/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alstudio.ui.module.image.browser;

import android.R.mipmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.utils.android.device.ALDisplayMetricsManager;
import com.alstudio.utils.log.ALLog;
import com.loovee.imaohu.R;

/**
 * This fragment will populate the children of the ViewPager from
 * {@link ImageDetailActivity}.
 */
public class ImageDetailFragment extends Fragment {
	private static final String IMAGE_DATA_EXTRA = "extra_image_data";
	private String mImageUrl;
	private ImageView mImageView;
	private Handler mHandler;

	/**
	 * Factory method to generate a new instance of the fragment given an image
	 * number.
	 * 
	 * @param imageUrl
	 *            The image url to load
	 * @return A new instance of ImageDetailFragment with imageNum extras
	 */
	public static ImageDetailFragment newInstance(String imageUrl) {
		final ImageDetailFragment f = new ImageDetailFragment();

		final Bundle args = new Bundle();
		args.putString(IMAGE_DATA_EXTRA, imageUrl);
		f.setArguments(args);

		return f;
	}

	/**
	 * Empty constructor as per the Fragment documentation
	 */
	public ImageDetailFragment() {
	}

	/**
	 * Populate image using a url from extras, use the convenience factory
	 * method {@link ImageDetailFragment#newInstance(String)} to create this
	 * fragment.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mImageUrl = getArguments() != null ? getArguments().getString(
				IMAGE_DATA_EXTRA) : null;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate and locate the main ImageView
		final View v = inflater.inflate(R.layout.image_detail_fragment,
				container, false);
		mImageView = (ImageView) v.findViewById(R.id.imageView);
		mImageView.setOnClickListener(mOnClickListener);
		
		FrameLayout.LayoutParams parm = new LayoutParams(ALDisplayMetricsManager.getScreenWidth(getActivity()), ALDisplayMetricsManager.getScreenWidth(getActivity()));
		parm.gravity = Gravity.CENTER;
		mImageView.setLayoutParams(parm);
		return v;
	}

	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			 mHandler.sendEmptyMessage(0);
		}
	};

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// ALLocalEnv.getLocalEnv().displayImagePluginServerlet(mImageUrl,
		// mImageView);

		// 普通放心显示配置
		// DisplayImageOptions normalImageDisplayOptions = new
		// DisplayImageOptions.Builder()
		// .cacheInMemory(true)
		// .cacheOnDisc(true)/* .considerExifParams(true) */
		// .bitmapConfig(Bitmap.Config.RGB_565)
		// .displayer(new FadeInBitmapDisplayer(200)) .build();

		// ALLocalEnv.getLocalEnv().displayImage(mImageUrl, mImageView,
		// normalImageDisplayOptions);

		// ALLocalEnv.getLocalEnv().displayImagePluginServerlet(mImageUrl,
		// mImageView,
		// normalImageDisplayOptions);
		ALLog.d("开始显示大图");
		ALLocalEnv.getLocalEnv().displayImagePluginServerlet(mImageUrl,
				mImageView,ALLocalEnv.getLocalEnv().getLargeIconImageDisplayOption());

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mImageView != null) {
			// Cancel any pending image work
			// ImageWorker.cancelWork(mImageView);
			mImageView.setImageDrawable(null);
		}
	}

	/**
	 * @param mHandler
	 *            the mHandler to set
	 */
	public void setmHandler(Handler mHandler) {
		this.mHandler = mHandler;
	}
}
